package com.example.pixelyear.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.pixelyear.R
import com.example.pixelyear.bean.ColorBean
import com.example.pixelyear.utils.extension.setOnDoubleClickListener
import com.socks.library.KLog
import kotlinx.android.synthetic.main.item_edit_color.view.*
import java.util.*

class EditColorAdapter(val context: Context) : BaseAdapter<ColorBean>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        return BaseViewHolder(LayoutInflater.from(context).inflate(R.layout.item_edit_color, parent, false))

    }

    override fun onBindView(itemView: View, bean: ColorBean, position: Int) {
        itemView.Item_EditColor_Color.setBackgroundColor(bean.color)
        itemView.Item_EditColor_Text.text = bean.text
        itemView.Item_EditColor_Text.setTextColor(bean.color)
        itemView.setOnDoubleClickListener {
            onItemClick?.click(bean, position)
        }
    }

    override fun onMove(before: Int, after: Int) {
        if (before < after) {
            for (i in before until after) {
                Collections.swap(l, i, i + 1)
            }
        } else {
            for (i in before downTo after + 1) {
                Collections.swap(l, i, i - 1)
            }
        }
        notifyItemMoved(before, after)
    }


    override fun onRemover(position: Int) {
        KLog.e("yxs", "触发了删除：$position")
        remove(position) {
            if (it.size <= 1) {
                canDrag = false
                canSwipe = false
            }
            onItemClick?.third(ColorBean(-1, "-1"), it.size)
        }
    }

}
